package com.voltmao.controller.module.common;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.voltmao.service.application.contract.ProviderServiceImpl;
import com.voltmao.service.application.doc.ApplyServiceImpl;
import com.voltmao.service.application.intent.IntentUserServiceImpl;
import com.voltmao.service.application.sys.MessageServiceImpl;
import com.voltmao.webpro.frame.analyze.DTOSmith;
import com.voltmao.webpro.frame.dto.ApiResult;
import com.voltmao.webpro.frame.dto.status.Constants;
import com.voltmao.webpro.frame.exception.WebProViewException;
import com.voltmao.webpro.utils.StringUtils;
import com.voltmao.webpro.utils.http.HttpRequestUtil;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping(value = "/module/homepage")
public class HomePageController {
    @Autowired
    IntentUserServiceImpl intentUserService;
    @Autowired
    ProviderServiceImpl providerService;
    @Autowired
    ApplyServiceImpl applyService;
    @Autowired
    MessageServiceImpl messageService;
    /**
     * 业务员首页数据
     * @param request
     * @return
     */
    @RequestMapping(value = "/yw.json")
    @ResponseBody
    public ApiResult homePageDataForYw(HttpServletRequest request){
        return new DTOSmith(request).builderApiResult(new DTOSmith.ApiLoadBackCall() {
            @Override
            public Object resultData(HttpServletRequest request, ApiResult apiResult) throws WebProViewException {
                Map<String, Object> params = HttpRequestUtil.getParameterMap(request);
                if(!StringUtils.isNotEmpty("emplId")){
                    apiResult.setStatus(Constants.PARAMETER_ERROR);
                    return null;
                }
                String emplId = params.get("emplId").toString();
                Map<String,Object> res = new HashMap<>(4);
                res.put("readyToFollow",intentUserService.readyToFollow(emplId));
                res.put("hasFollowed",intentUserService.hasFollowed(emplId));
                Map<String,Object> tmpParam = new HashMap<>(4);
                tmpParam.put("userTo",emplId);
                res.put("msg",messageService.selectDataPageMsgs(tmpParam));
                return res;
            }
        });
    }

    @RequestMapping(value = "/zj.json")
    @ResponseBody
    public ApiResult homePageDataForZj(HttpServletRequest request){
        return new DTOSmith(request).builderApiResult((request1, apiResult)->{
            int docApplyCounts = applyService.countToBeAudit();
            //将待分配渠道商计数放进来
            Map<String,Object> res = new HashMap<>(4);
            res.put("readyToAssign", intentUserService.countUnAssign());
            res.put("docApply",docApplyCounts);
            res.put("contractToAudit",providerService.countToBeAudit(0));
            res.put("contractToSure",providerService.countToBeAudit(2));
            return res;
        });
    }
}
